var AlsSdk = require("utils/alssdk.js");

App({
    onLaunch: function(opt) {
        console.log(opt)
        var t = this
        var share_uid=opt.query.uid
        
        t.loadConfig()
            qq.login({
            success: function(res) {
                console.log(res)
                if(res.code){
                AlsSdk.getOpenid(res.code).then(rest => {
                 
                  
                     wx.setStorageSync('openid',rest);
                     if(share_uid){
                         wx.setStorageSync('share_uid',share_uid)
                     }
                    t.login(rest,share_uid)
           })
          }
            }
        }), wx.getSetting({
            success: function(e) {
                e.authSetting["scope.userInfo"] && wx.getUserInfo({
                    success: function(e) {
                        t.userInfoReadyCallback && t.userInfoReadyCallback(e);
                    }
                });
            }
        });
    },
    mylogin(share_uid=''){
        var t=this
     
    },
    login(openid='',share_uid='') {

        var score =50;
        var that=this;
        if (openid) {
          let userparam = {
            'openid': openid,
          }
        //   var myDate = new Date();
        //   var day=myDate.toLocaleDateString();
          wx.Als.bmobFunc('vote_user', 'all', userparam).then(res => {
            console.log(res[0])
            if (res.length > 0) {
    
              let uid = res[0].objectId
              
              wx.setStorageSync('uid', uid)
              score = res[0].score
              if (score < 0) {
                score = 0
              }
              
              wx.setStorageSync('nickName', res[0].nickName)
              wx.setStorageSync('uimg', res[0].avatarurl)
              wx.setStorageSync('score', score)
             wx.setStorageSync('level',res[0].level)
             wx.setStorageSync('share_num', res[0].share_num)
          
      
            
            } else {
                if(!share_uid){
                    share_uid = wx.getStorageSync('share_uid')
                }
                if(share_uid){
                    that.add_share(share_uid)
                }
              
              var username = wx.getStorageSync('myname')
              var uimg = wx.getStorageSync('myurl')
              var sex = wx.getStorageSync('sex')
              if (sex != 1) {
                sex = 0
              }
              wx.setStorageSync('mysex', sex)
              let param = {
                sex: sex,
                uimg: uimg,
                avatarurl:'',
                nickName:username,
                openid: openid,
                score: score,
                vip: false,
                isshow: true,
                share_uid: share_uid,
                share_num: 0,
                vote_num:0,

              
    
              }
              wx.Als.bmobFunc('vote_user', 'add', param).then(res => {
                let uid = res.objectId
                wx.setStorageSync('uid', uid)
                wx.setStorageSync('score', score)
                wx.setStorageSync('share_num', 0)
                 
              })
            }
          })
    
        }
      },
         addVoteNum(uid=''){
        var that=this
        if(!uid){uid=wx.getStorageSync('uid')}
    let param={
      'id':uid,
      'field':'vote_num',
    }
    wx.Als.bmobFunc('vote_user','inc',param).then(res=>{
        console.error('usershare=')
      console.log(res)
     
    })
  
  
  },
      getRandomArrayElements(arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    },
      add_share(share_uid){
        var that=this
    let param={
      'id':share_uid,
      'field':'share_num',
    }
    wx.Als.bmobFunc('vote_user','inc',param).then(res=>{
        console.error('usershare=')
      console.log(res)
      if(res.share_num>29&&res.share_num<32){
          that.updateShareUser(share_uid);
      }
    })
  
  
  },
  updateShareUser(attr){
      var that=this;
          if(!attr){return}
        let param={
            level:1
        }
        
        wx.Als.bmobFunc('vote_user', 'get_one', param,attr).then(res => {
              console.error('sharelevel=')
            console.log(res)
           
        })
  },
    wxlogin: function() {
        var t = wx.getStorageSync("openid");
     
    },
    loadConfig: function() {
      var n=this;
      let param = {
        'is_show': true
      }
      wx.Als.bmobFunc('vote_setting', 'all', param).then(res => {

        if (res.length > 0) {
          n.config=res[0];
       
        
        }
      })
      
    },
    config:{},
      arrList: ['投了一票','选择了红方','发布了一个问题','投了一票','分享给好友','投了一票','投了一票','分享给好友','发布了一个问题','查看了问题','查看了问题','选择了红方','选择了蓝方','查看最新话题','查看排行榜','选择了蓝方','选择了红方','综合分数提升了','查看了问题','查看了问题','投了一票','查看排行榜','投了一票','分享给好友','生成了分享图','成为了审核','的问题已通过审核','投了一票','感谢投票','支持了作者','选择了蓝方','投了一票','投了一票','看了AD','分享了问题','登上排行榜','查看了投票结果','感谢投票','邀请了好友','分享了问题','查看了投票结果','查看了投票结果','综合分数提升了','查看了问题','参与了打卡','打卡啦','打卡啦'],
        arrCate: ['刚刚', '30秒前', '40秒前','1分钟前','3分钟前','5分钟前','3分钟前','5分钟前','15分钟前','30分钟前','1小时前','刚刚','30秒前', '40秒前','1分钟前','30秒前', '40秒前','1分钟前','15分钟前','10分钟前','50分钟前','20分钟前'],
    globalData: function(t, e, a) {
        return e in t ? Object.defineProperty(t, e, {
            value: a,
            enumerable: !0,
            configurable: !0,
            writable: !0
        }) : t[e] = a, t;
    }({
        is_content_true: !1,
        userInfo: null,
        detail_id: "",
        from_vote: "",
        api_url: ""
    }, "from_vote", 0)
});